// import { * } from './myModule.js';
const canvas = document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
const spriteWidth = 575; // 精灵图片像素大小
const spriteHeight = 523; // 精灵图片像素大小
const CANVAS_WIDTH = canvas.width = spriteWidth;
const CANVAS_HEIGHT = canvas.height = spriteHeight;

const playerImage = new Image();
playerImage.src = '../media/img/shadow_dog.png'

let frameIndex = 0; // 精灵切片

let gameFrame = 0;  // 播放速度
const staggerFrames = 5;

const animationStatesForNumber = {
    0: "空闲",
    1: "跳跃",
    2: "下落",
    3: "跑步",
    4: "眩晕",
    5: "蹲下",
    6: "滚动",
    7: "攻击",
    8: "闪避",
    9: "受到攻击"
}
const animationStates = {
    '空闲': { // 空闲
        frames: 7,
        loc: []
    },
    '跳跃': {// 跳跃
        frames: 7,
        loc: []
    },
    '下落': {// 下落
        frames: 7,
        loc: []
    },
    '跑步': {// 跳跃
        frames: 7,
        loc: []
    },

    '眩晕': {// 跳跃
        frames: 11,
        loc: []
    },
    '蹲下': {// 跳跃
        frames: 5,
        loc: []
    },
    '滚动': {// 跳跃
        frames: 7,
        loc: []
    },
    '攻击': {// 跳跃
        frames: 7,
        loc: []
    },
    '闪避': {// 跳跃
        frames: 12,
        loc: []
    },
    '受到攻击': {// 跳跃
        frames: 4,
        loc: []
    }
}


let playerStatus = '空闲';
const dropdown = document.getElementById('animations');
dropdown.addEventListener('change', function (e) {
    playerStatus = e.target.value;
    console.log(playerStatus);
    
})


function computeloc() {
    for (let index = 0; index < Object.keys(animationStates).length; index++) {
        for (let index2 = 0; index2 <= animationStates[animationStatesForNumber[index]].frames; index2++) {
            animationStates[animationStatesForNumber[index]].loc.push({ x: index2 * spriteWidth, y: index * spriteHeight });
        }
    }
}

function animate() {
    ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
    ctx.drawImage(playerImage, animationStates[playerStatus].loc[frameIndex].x, animationStates[playerStatus].loc[frameIndex].y, spriteWidth, spriteHeight, 0, 0, spriteWidth, spriteHeight);
    if (gameFrame % staggerFrames == 0) {
        frameIndex = (frameIndex + 1) % animationStates[playerStatus].frames;
    }
    gameFrame += 1;
    requestAnimationFrame(animate);
}

computeloc();

animate('空闲');